//
//  MsgNotificationViewController.m
//  Kaiser
//
//  Created by ZhangYeKun on 2017/3/19.
//  Copyright © 2017年 wei wang. All rights reserved.
//

#import "MsgNotificationViewController.h"
#import "MsgNotificationTableViewCell.h"
//#import "JushModel.h"
#import "GetAlertListApi.h"
#import "GetMessageList.h"
#import "MessageModel.h"
#import "NSString+FIDDate.h"

@interface MsgNotificationViewController ()<UITableViewDelegate,UITableViewDataSource>
/** tableView **/
@property(nonatomic,strong) UITableView *tableView;

/** array **/
@property(nonatomic,strong) NSMutableArray *array;

@property(nonatomic,assign)int page;
@property(nonatomic,assign)NSInteger type;

@end

@implementation MsgNotificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavigationStyle];
    self.view.backgroundColor = [UIColor colorWithRed:245/255.0 green:246/255.0 blue:247/255.0 alpha:1];
    
//    NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
//    //还要指定存储文件的文件名称,仍然使用字符串拼接
//    NSString *filePath = [docPath stringByAppendingPathComponent:@"MeasgeHistoryArray.plist"];
//    NSArray *readArray = [NSArray arrayWithContentsOfFile:filePath];
//    for (NSDictionary *info in readArray) {
//
//        NSLog(@"%@",info[@"time"]);
//        JushModel *obc = [[JushModel alloc] init];
//        obc.time = info[@"time"];
//        obc.alert = info[@"alert"];
//        [self.array addObject:obc];
//    }
//    [self.tableView reloadData];

    self.page = 1;
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)updateDataSource {
    __weak typeof(self) weak = self;
    GetMessageList *api = [[GetMessageList alloc]initWithDic:@{
                                                               @"device_type":@"0",
                                                               @"page":[NSString stringWithFormat:@"%d",self.page]
                                                               }];
    [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
        NSDictionary *dic = request.responseJSONObject;
        
        if ([dic[@"errcode"] intValue] == 0) {
            NSMutableArray * childList = [NSMutableArray new];
            if (dic[@"data"] && [dic[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray *list = dic[@"data"];
                for (NSDictionary *infoDic in list) {
                    MessageModel *model = [[MessageModel alloc]init];
                    [model setValuesForKeysWithDictionary:infoDic];
                    [childList addObject:model];
                }
            }
            
            if (weak.page == 1)
            {
                [weak.array removeAllObjects];
                [weak.array addObjectsFromArray:childList];
                [weak.tableView reloadData];
            }
            else
            {
                if (childList.count != 0)
                {
                    [weak.array addObjectsFromArray:childList];
                    [weak.tableView reloadData];
                }
                else
                {
                    [weak.tableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
        }
        
        
    } failure:^(YTKBaseRequest * _Nonnull request) {
        [self.tableView.mj_footer endRefreshing];
        [self.tableView.mj_header endRefreshing];
    }];
}


- (NSMutableArray *)array{
    
    if (!_array) {
        
        _array = [NSMutableArray array];
    }
    
    return _array;
}

- (void)onLeftButtonClick:(id)sender{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

#pragma mark - --------------------------UITableView dataSource--------------------------

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.array.count;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MsgNotificationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MsgNotificationTableViewCell"];

    cell.backgroundColor = [UIColor clearColor];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    MessageModel *info = self.array[indexPath.section];
    cell.dateLabel.text = [NSString getYearDateWithDaySince1970:[info.ctime integerValue] dateFormat:@"yyyy-MM-dd HH:mm"];
    cell.msgLabel.text = info.content;
    return cell;
    
}
#pragma mark - --------------------------UITableView delegate--------------------------


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return CGFLOAT_MIN;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
}

- (UITableView *)tableView{
    
    if (!_tableView) {
        
        __weak UIView *superView = self.view;
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        [superView addSubview:_tableView];
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;//隐藏tableView的分割线
        //        _tableView.scrollEnabled = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"MsgNotificationTableViewCell" bundle:nil] forCellReuseIdentifier:@"MsgNotificationTableViewCell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.page = 1;
            [self updateDataSource];
        }];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page ++;
            [self updateDataSource];
        }];
        
    }
    
    return _tableView;
    
}


- (void)setNavigationStyle{
    
    [self setNavigationTitle:@"消息通知" titleColor:nil];
    [self setLeftButtonImage:[UIImage imageNamed:@"icon_back_nor"]];

}

- (void)masLayoutSubview{
    
    __weak UIView *superView = self.view;
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(superView);
    }];
    self.tableView.estimatedRowHeight = 213;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
